package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.db.Topic;
import com.itheima.domain.db.TopicQuestion;
import com.itheima.mapper.QuestionForSoulMapper;
import com.itheima.mapper.TopicMapper;
import com.itheima.service.db.QuestionForSoulService;
import com.itheima.service.db.TopicService;
import com.itheima.vo.QuestionsVo;
import com.itheima.vo.TopicVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    private final QuestionForSoulService questionForSoulService;

    public List<TopicVo> findTopics(){
        //查出List<Topic>
        List<Topic> list = lambdaQuery().list();
        List<TopicVo> topicVos = new ArrayList<>();

        if(list == null || list.isEmpty()){
            return Collections.emptyList();
        }

        //将List<Topic>组装成List<TopicVo>
        for (Topic topic : list) {
            TopicVo topicVo = new TopicVo();
            BeanUtils.copyProperties(topic,topicVo);
            //看Star的值是否为1
            if(topic.getStar().equals("1")){
                topicVo.setIsLock(0);
            }else {
                topicVo.setIsLock(1);
            }
            topicVos.add(topicVo);
        }
        //接着组装topicvo的对应question集合,然后返回List<TopicVo>
        for (TopicVo topicVo : topicVos) {
            //根据topicId查出对应的question集合
            String id = topicVo.getId();
            List<QuestionsVo> topicQuestions = questionForSoulService.findQuestionsByTopicId(id);
            if(topicQuestions == null || topicQuestions.isEmpty()){
                topicVo.setQuestions(Collections.emptyList());
            }
            topicVo.setQuestions(topicQuestions);
        }
        return topicVos;
    }
}
